﻿using System;
using System.Collections;
using System.Linq;

namespace Deanery.Controller.Directories
{
    class TypePrivilegeController
    {
        private readonly deaneryEntities _db = new deaneryEntities();


        /// <summary>
        /// This method transfers columns name in ukrainian for view table Type_Privilege
        /// </summary>
        /// <returns>Array of string with columns names</returns>
        public Array ColumnName()
        {
            string[] columnName = { "ID", "Вид пільги", "Код ЄДБО","Група" };
            return columnName;
        }

        /// <summary>
        /// This method transfers columns data type in table Type_Privilege
        /// </summary>
        /// <returns>Array of string with columns data type</returns>
        public Array ColumnValueType()
        {
            string[] columnDataType = { "guid", "string", "string", "guid" };
            return columnDataType;
        }

        /// <summary>
        /// This method select all reccords from table Type_Privilege
        /// </summary>
        /// <returns>Set of IQueryable</returns>
        public IQueryable GetAll()
        {
            var list = _db.Type_Privilege.Join(_db.Privilege_Group, t => t.id_privilege_group, f => f.id_privilege_group,
                                               (t, f) =>
                                               new
                                                   {
                                                       t.id_privilege_type,
                                                       t.nv_privilege_type_name,
                                                       t.int_privilege_edbo,
                                                       f.nv_privilege_group_name
                                                   }); 
            
            return list;
        }

        /// <summary>
        /// This method select all privilege group name from table Privilege_Group
        /// idea of this method: to return data from all conected tables with this one
        /// </summary>
        /// <returns>Array(every item is test name)</returns>
        public Array GetConectedData()
        {
            var list = from t in _db.Privilege_Group select t.nv_privilege_group_name;
            return list.ToArray();
        }

        /// <summary>
        /// This method performs INSERT one record to DB in table Type_Privilege
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param>
        public void Insert(ArrayList reccord)
        {
            var privilegeGroupObj = new PrivilegeGroupController(); 
            var obj = new Type_Privilege
            {
                id_privilege_type = GenerateGuid(),
                nv_privilege_type_name = reccord[0].ToString(),
                int_privilege_edbo = Convert.ToInt32(reccord[1].ToString()),
                id_privilege_group = privilegeGroupObj.FindGuidByName(reccord[2].ToString())
                
            };
            _db.Type_Privilege.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method performs UPDATE one record to DB in table Type_Privilege
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="updateGuid">guid value to search reccord in table</param>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to update</param>
        public void Update(Guid updateGuid, ArrayList reccord)
        {
            var privilegeGroupObj = new PrivilegeGroupController(); 
            var obj = (_db.Type_Privilege.Where(t => t.id_privilege_type == updateGuid)).Single();
            obj.nv_privilege_type_name = reccord[0].ToString();
            obj.int_privilege_edbo = Convert.ToInt32(reccord[1].ToString());
            obj.id_privilege_group = privilegeGroupObj.FindGuidByName(reccord[2].ToString());
            _db.SaveChanges();
        }

        /// <summary>
        /// This method generates new guid
        /// </summary>
        /// <returns>New guid</returns>
        public Guid GenerateGuid()
        {
            var guid = Guid.NewGuid();
            return guid;
        }

        /// <summary>
        /// This method select guid from table Type_Privilege
        /// </summary>
        /// <param name="name">String value for search guid in table</param>
        /// <returns>Guid of passed parameter</returns>
        public Guid FindGuidByName(string name)
        {
            var guid = (_db.Type_Privilege.Where(t => t.nv_privilege_type_name == name).Select(t => t.id_privilege_type)).SingleOrDefault(); 
            return guid;
        }
    }
}
